package cn.edu.sjtu.flaw_detection.service.impl;

import cn.edu.sjtu.flaw_detection.dto.StateDTO;
import cn.edu.sjtu.flaw_detection.dto.TaskDTO;
import cn.edu.sjtu.flaw_detection.service.FlawDetectionService;
import cn.edu.sjtu.flaw_detection.vo.TaskVO;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author loumoon
 * @date 2019-12-01 19:16
 */
@Service("real")
public class FlawDetectionServiceImpl implements FlawDetectionService {
  @Autowired private RestTemplate restTemplate;

  @Value("${flaw.detection.data.source.url}")
  private String dataSourceUrl;

  @Value("${flaw.detection.identifier}")
  private Integer flawDetectionIdentifier;

  public JSONObject detectStatus() {
    ResponseEntity<JSONObject> responseEntity =
        restTemplate.getForEntity(
            dataSourceUrl + "?num={1}", JSONObject.class, flawDetectionIdentifier);
    return responseEntity.getBody();
  }

  public JSONObject initiate() {
    JSONObject requestParam = new JSONObject();
    requestParam.put("num", flawDetectionIdentifier);
    ResponseEntity<JSONObject> responseEntity =
        restTemplate.postForEntity(dataSourceUrl, requestParam, JSONObject.class);
    return responseEntity.getBody();
  }

  @Override
  public void addTaskToPool(TaskDTO task) {}

  @Override
  public StateDTO queryECNState() {
    return null;
  }

  @Override
  public StateDTO queryTaskState(String processId, String taskId) {
    return null;
  }

  @Override
  public List<TaskVO> listTaskQueue() {
    return null;
  }

  @Override
  public boolean startTask(TaskDTO taskDTO) {
    return false;
  }

  @Override
  public boolean endTask(TaskDTO taskDTO) {
    return false;
  }

  @Override
  public Map<Integer, List<TaskVO>> retTasksPool() {
    return null;
  }
}
